package com.haiyou.dc.mq.listener;

import com.haiyou.dc.mq.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 产品消息消费服务
 *
 * @author huangzl
 */
@Component
@Slf4j
public class ProductConsumerListener {

    @Autowired
    private ProductService productService;

    @RabbitListener(queues = "${mq.basic.product.queue}", containerFactory = "singleListenerContainer")
    public void handleMessage(@Payload byte[] message) throws IOException {
        if (message != null) {
            String mobile = new String(message, "UTF-8");
            log.info("-------------------------------->接收产品消息，手机号={}", mobile);
            productService.updateProduct(mobile);
        }
    }


}
